package com.shujia.bigdata.map;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class FileRead {
    public static void main(String[] args) {
        /**
         * 反射和泛型可以干什么
         * 可以将代码变得更加通用
         *
         */
        ArrayList<Score> read = read("java/data/score.txt", Score.class);
        for (Score score : read) {
            System.out.println(score);
            Integer score1 = score.getScore();
        }


        ArrayList<Student> students = read("java/data/students.txt", Student.class);

        for (Student student : students) {
            System.out.println(student);
        }

    }

    /**
     * 读取文件工具
     *
     * @param path 文件路径
     * @return 返回一个集合
     */
    public static <T> ArrayList<T> read(String path, Class<T> tClass) {

        ArrayList<T> list = new ArrayList<>();

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileReader = new FileReader(path);
            bufferedReader = new BufferedReader(fileReader);


            //按行读取数据
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");


                /**
                 * 构建T类型的对象
                 */
                T t = tClass.newInstance();
                //对属性进行赋值

                //1、获取所有属性
                Field[] fields = tClass.getDeclaredFields();

                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];

                    String fieldName = field.getName();

                    Class<?> type = field.getType();

                    //构建属性设置值得方法名  setName   name   setName
                    String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);


                    if (type == Integer.class) {
                        //获取方法对象
                        Method method = tClass.getMethod(methodName, Integer.class);
                        //执行方法
                        method.invoke(t, Integer.parseInt(split[i]));
                    } else {
                        //获取方法对象
                        Method method = tClass.getMethod(methodName, String.class);
                        //执行方法
                        method.invoke(t, split[i]);
                    }


                }


                list.add(t);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //关闭流
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


        return list;

    }

}
